package com.googlecode.gwtbb.client.shared.entity.user;

import com.googlecode.gwtbb.client.shared.entity.properties.GetName;
import com.googlecode.gwtbb.client.shared.entity.properties.HasSnapDate;
import com.googlecode.gwtbb.client.shared.entity.properties.HasUserId;
import com.googlecode.gwtbb.client.shared.entity.properties.Lengths;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jun 17, 2008
 */
public class UserBase implements Lengths, HasSnapDate, HasUserId, GetName, Serializable {
	public static final String USERNAME = "username";
	public static final int USERNAME_MIN = 3;
	public static final int USERNAME_MAX = 30;
	public static final String PASSWORD = "password";
	public static final int PASSWORD_MIN = 6;
	public static final int PASSWORD_MAX = 60;
	public static final String EMAIL = "email";
	public static final int EMAIL_MIN = 6;
	public static final int EMAIL_MAX = 100;
	public static final String PRIVILEGE_LEVEL = "privilegeLevel";

	public static final List<String> PROPERTIES = Collections.unmodifiableList(Arrays.asList(
		UserBase.USER_ID,
		UserBase.USERNAME,
		UserBase.PASSWORD,
		UserBase.EMAIL,
		UserBase.PRIVILEGE_LEVEL,
		UserBase.SNAP_DATE));

	protected Long userId;
	protected String username;
	protected String password;
	protected String email;
	protected PrivilegeLevel privilegeLevel;
	protected Date snapDate;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public PrivilegeLevel getPrivilegeLevel() {
		return privilegeLevel;
	}

	public void setPrivilegeLevel(PrivilegeLevel privilegeLevel) {
		this.privilegeLevel = privilegeLevel;
	}

	public Date getSnapDate() {
		return snapDate;
	}

	public void setSnapDate(Date snapDate) {
		this.snapDate = snapDate;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getName() {
		return username;
	}

	public boolean isAdmin() {
		return privilegeLevel != null && privilegeLevel.ordinal() >= PrivilegeLevel.ADMIN.ordinal();
	}

	public boolean isMember() {
		return privilegeLevel != null && privilegeLevel.ordinal() >= PrivilegeLevel.MEMBER.ordinal();
	}

	public boolean isModerator() {
		return privilegeLevel != null && privilegeLevel.ordinal() >= PrivilegeLevel.MODERATOR.ordinal();
	}

	public boolean isRegistered() {
		return privilegeLevel != null && privilegeLevel.ordinal() > PrivilegeLevel.ANONYMOUS.ordinal();
	}

	// don't change the order of enums
	public enum PrivilegeLevel {
		ANONYMOUS,
		UNVERIFIED,
		MEMBER,
		MODERATOR,
		ADMIN
	}
}
